#include "mydialog.h"
#include <QWidget>
#include <QPixmap>
#include <QPushButton>
#include <QDateTime>
#include <QTimer>
#include <QDebug>
#include <QTimerEvent>

MyDialog::MyDialog(QWidget *parent) : QDialog(parent), m_TimerID(0)
{
    BuildUI();

    QTimer *pTimer = new QTimer(this);
    connect(pTimer, SIGNAL(timeout()), this, SLOT(updateTime()));
    pTimer->start(1000);
    //pTimer->stop();

    connect(pOkBtn, SIGNAL(clicked()), this, SLOT(close()));
    connect(pCancelBtn, SIGNAL(clicked()), this, SLOT(close()));

    m_TimerID = this->startTimer(1000);
}

MyDialog::~MyDialog()
{

}

void MyDialog::AdjustBigHeap(QVector<int> &buf, int start, int end)
{
    int tmp = buf[start];
    int index = 2*start+1, c = start;

    for(; index <= end; c = index, index = 2*index + 1) {
        if(index < end && buf[index] < buf[index+1])
            index++;
        if(tmp >= buf[index])
            break;
        else {
            buf[c] = buf[index];
            buf[index] = tmp;
        }
    }
}

void MyDialog::HeapSort()
{
    QVector<int> sortBuf = {3, 6, 7, 9, 4, 54, 12, 1};

    int len = sortBuf.length();

    for(int i = len/2 - 1; i >=0; i--)
        AdjustBigHeap(sortBuf, i, len - 1);

    QVector<int> outBuf;
    for(int i = len - 1; i > 0; i--) {
        int tmp = sortBuf[0];
        sortBuf[0] = sortBuf[i];
        sortBuf[i] = tmp;
        outBuf.append(tmp);
        AdjustBigHeap(sortBuf, 0, i - 1);
    }
    outBuf.append(sortBuf[0]);

    for(auto t : outBuf) {
        qDebug() << t << " ";
    }
}

void MyDialog::timerEvent(QTimerEvent *event)
{
    if(event->timerId() == m_TimerID) {
        qDebug() << "QObject time eg";
        HeapSort();
        this->killTimer(m_TimerID);
    }
}

void MyDialog::BuildUI()
{
    setWindowTitle(QStringLiteral("Qt布局"));

    //===================================================================
    //layout left
    pLeftLayout = new QGridLayout;

    pUsrNameLabel    = new QLabel(QStringLiteral("用户名:"));
    pUsrNameLineEdit = new QLineEdit;

    pNameLabel       = new QLabel(QStringLiteral("姓名:"));
    pNameLineEdit    = new QLineEdit;

    pSexLabel        = new QLabel(QStringLiteral("性别:"));
    pSexComboBox     = new QComboBox;
    pSexComboBox->addItem(QStringLiteral("男"));
    pSexComboBox->addItem(QStringLiteral("女"));

    pDepartmentLabel   = new QLabel(QStringLiteral("部门:"));
    pDepartmentTxtEdit = new QTextEdit;

    pAgeLabel        = new QLabel(QStringLiteral("年龄:"));
    pAgeLineEdit     = new QLineEdit;

    pOtherLabel      = new QLabel(QStringLiteral("备注:"));
    pOtherLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken);

    pLeftLayout->addWidget(pUsrNameLabel, 0, 0);
    pLeftLayout->addWidget(pUsrNameLineEdit, 0, 1);
    pLeftLayout->addWidget(pNameLabel, 1, 0);
    pLeftLayout->addWidget(pNameLineEdit, 1, 1);
    pLeftLayout->addWidget(pSexLabel, 2, 0);
    pLeftLayout->addWidget(pSexComboBox, 2, 1);
    pLeftLayout->addWidget(pDepartmentLabel, 3, 0);
    pLeftLayout->addWidget(pDepartmentTxtEdit, 3, 1);
    pLeftLayout->addWidget(pAgeLabel, 4, 0);
    pLeftLayout->addWidget(pAgeLineEdit, 4, 1);
    pLeftLayout->addWidget(pOtherLabel, 5, 0, 1, 2);

    pLeftLayout->setColumnStretch(0, 1);   //0列与1列宽度对比1:3
    pLeftLayout->setColumnStretch(1, 3);

    //===================================================================
    //layout right
    //layout topright
    pTopRightLayout = new QHBoxLayout;

    pHeadLabel      = new QLabel(QStringLiteral("头像"));
    pHeadIconLabel  = new QLabel;
    pRefreshHeadBtn = new QPushButton(QStringLiteral("更新"));

    QPixmap pic("./icon/nofont.png"); //".\\icon\\nofont.png"
    pHeadIconLabel->setPixmap(pic);
    pHeadIconLabel->resize(pic.width(), pic.height());

    pTopRightLayout->setSpacing(20);  //各控件间隔
    pTopRightLayout->addWidget(pHeadLabel);
    pTopRightLayout->addWidget(pHeadIconLabel);
    pTopRightLayout->addWidget(pRefreshHeadBtn);

    pRightLayout = new QVBoxLayout;

    pIntroductionLabel = new QLabel(QStringLiteral("个人介绍:"));
    pIntroductionTxtEdit = new QTextEdit;

    pRightLayout->addLayout(pTopRightLayout);
    pRightLayout->addWidget(pIntroductionLabel);
    pRightLayout->addWidget(pIntroductionTxtEdit);

    //===================================================================
    //bottom layout
    pBottomLayout = new QHBoxLayout;

    pTimerLabel = new QLabel;
    pOkBtn = new QPushButton(QStringLiteral("确定"));
    pCancelBtn = new QPushButton(QStringLiteral("取消"));

    pBottomLayout->addWidget(pTimerLabel);
    pBottomLayout->addStretch();  //占位
    pBottomLayout->addWidget(pOkBtn);
    pBottomLayout->addWidget(pCancelBtn);

    //====================================================================
    //main layout
    QGridLayout *mainlayout = new QGridLayout(this);

    mainlayout->setSpacing(10);
    mainlayout->setMargin(10);
    mainlayout->addLayout(pLeftLayout, 0, 0);
    mainlayout->addLayout(pRightLayout, 0, 1);
    mainlayout->addLayout(pBottomLayout, 1, 0, 1, 2);
    mainlayout->setSizeConstraint(QLayout::SetFixedSize); //固定窗口
}

void MyDialog::updateTime()
{
    QDateTime date = QDateTime::currentDateTime();
    QString str = date.toString("yyyy-MM-dd hh:mm:ss dddd");

    pTimerLabel->setText(str);
}
